package net.bwie.order.dwd.function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.guanjuntao.util.HbaseUtil;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;

import java.util.Map;

public class LoadProvinceDimMapFunction extends RichMapFunction<String, String> {

    // TODO: 2025/6/4 定义map结果,key为关联字段
    private Map<String, String> cacheMap;

    @Override
    public void open(Configuration parameters) throws Exception {
        // TODO: 2025/6/4 获取Hbase表中的省份维度数据,存储到Map集合中
        cacheMap = HbaseUtil.scanData("dim_base_province","info","name");
    }

    @Override
    public String map(String value) throws Exception {
        // TODO: 2025/6/4 解析json数据
        JSONObject jsonObject = JSON.parseObject(value);
        // TODO: 2025/6/4 获取关联字段值
        String provinceId = jsonObject.getString("province_id");
        // TODO: 2025/6/4 从Map集合获取对应Value值
        String provinceName = cacheMap.getOrDefault(provinceId, "未知");
        // TODO: 2025/6/4 添加字段
        jsonObject.put("province_name", provinceName);
        return jsonObject.toJSONString();

    }

 @Override
    public void close() throws Exception {
        HbaseUtil.close();
    }






















}
